/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.pridexmltomgfconverter.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.zip.GZIPInputStream;
import org.apache.log4j.Logger;

/**
 *
 * @author Kenneth
 */
public class GunZipper {

    private static GunZipper gunZipper = null;
    private static final Logger logger = Logger.getLogger(GunZipper.class);
    private static int bufferSize = 4096;

    public static GunZipper getInstance() {
        if (gunZipper == null) {
            gunZipper = new GunZipper();
        }
        return gunZipper;
    }

    private GunZipper() {
    }

    public void setBufferSize(int bufferSize) {
        this.bufferSize = bufferSize;
        BufferSize size = BufferSize.b;
        while (bufferSize > 1024) {
            bufferSize = bufferSize / 1024;
            size = size.getNext();
        }
        logger.debug("Set buffersize to " + bufferSize + " " + size);
    }

    public int getBufferSize() {
        int xSize = bufferSize;
        BufferSize size = BufferSize.b;
        while (xSize > 1024) {
            xSize = xSize / 1024;
            size = size.getNext();
        }
        logger.debug("Buffersize is currently " + xSize + " " + size);
        return this.bufferSize;
    }

    private enum BufferSize {

        b, kb, mb, gb, tb;

        public BufferSize getNext() {
            return values()[(ordinal() + 1) % values().length];
        }
    }

    public File unzip(File zippedFile, File destinationFile) throws IOException {
        InputStream nin = null;
        OutputStream out = null;
        GZIPInputStream in = null;
        File tempFile = new File(destinationFile.getAbsolutePath().replace(".mgf", "temp.xml"));
        try {
            nin = new FileInputStream(zippedFile);
            out = new FileOutputStream(tempFile);
            in = new GZIPInputStream(nin, bufferSize);
            byte[] buffer = new byte[bufferSize];
            int noRead;
            int readCount = 0;
            int lineCount = 0;
            logger.info("Unzipping" + zippedFile.getName());
            while ((noRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, noRead);
            }
            in.close();
            out.close();
        } catch (Exception e) {
            logger.error("An error has occurred while zipping...");
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (Exception e) {
                    out = null;
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e) {
                    in = null;
                }
            }
            return tempFile;
        }
    }
}